package admin.action;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;

import home.model.Msg;
import home.model.PageBean;
import home.service.MsgService;

import com.opensymphony.xwork2.ActionSupport;

public class MsgMngAction extends ActionSupport{
	private MsgService msgs;
	private String type;
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	private List<Msg> msgList;
	private Msg msgEdit;
	public Msg getMsgEdit() {
		return msgEdit;
	}
	public void setMsgEdit(Msg msgEdit) {
		this.msgEdit = msgEdit;
	}
	private int id;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public List<Msg> getMsgList() {
		return msgList;
	}
	public void setMsgList(List<Msg> msgList) {
		this.msgList = msgList;
	}
	public MsgService getMsgs() {
		return msgs;
	}
	public void setMsgs(MsgService msgs) {
		this.msgs = msgs;
	}
	private int page;
	private PageBean pageBean;
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public PageBean getPageBean() {
		return pageBean;
	}
	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}
	private String like;
	public String getLike() {
		return like;
	}
	public void setLike(String like) {
		this.like = like;
	}
	public String execute() throws Exception{
		pageBean = msgs.queryForPage(8, page, type, like);
		msgList = pageBean.getList();
		System.out.println(msgList);
		return SUCCESS;
	}
	public String add(){
		return SUCCESS;
	}
	public String addSubmit(){
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date(System.currentTimeMillis());
		msgEdit.setCreated_at(df.format(date));
		msgEdit.setCreated_by("MsgMngAction/addSubmit");
		msgEdit.setUpdated_at(df.format(date));
		msgEdit.setUpdated_by("MsgMngAction/addSubmit");

		msgs.saveMsg(msgEdit);
		return SUCCESS;
	}
	public String edit(){
		msgEdit = msgs.getMsg(id);
		if(msgEdit == null) return INPUT;
		return SUCCESS;
	}
	public String editSubmit(){
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date(System.currentTimeMillis());
		msgEdit.setUpdated_at(df.format(date));
		msgEdit.setUpdated_by("MsgMngAction/addSubmit");
		System.out.println(msgEdit.getContent());

		msgs.saveMsg(msgEdit);
		return SUCCESS;
	}
	public String delete(){
		msgs.deleteMsgById(id);
		return SUCCESS;
	}
}
